home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / 4cmp22s.zip / DEMO.4TH < prev    next >
Text File  |  1994-10-30  |  5KB  |  127 lines

  1. \ This simple program illustrates the basics of a Forth program prepared
  2. \ for ForthCMP. Comments can be anything after a backslash,
  3. ( or can be text within parenthesis on a single line )
  4.  
  5. \ This version of ForthCMP (2.1s) supports most of the ANSI Forth standard.
  6. \ Another available version (2.1a) supports the 1983 Forth Standard
  7.  
  8. 0 [IF]  Interpreted conditionals allow multiline comments, among other
  9.     things, without having to bother with the comment characters.
  10.  
  11.     Lines can be up to 127 characters long, and spaces and tabs are
  12.     ignored.
  13.  
  14.     To compile this program, the compiler, 4C.COM, must be in the
  15.     execution path, and the files DOSGO.4TH and FORTHLIB.4TH must be
  16.     either in the current directory or in a directory pointed to by
  17.     the environment varible 4LIB. For example, "SET 4LIB=d:\FCLIB"
  18.     will cause the directory d:\fclib to be used for file searches
  19.     if the file is not found in the current directory. These library
  20.     files, as all input files, are in source format. Source files have
  21.     the default extensions 4TH or SCR. Either ASCII text files, such
  22.     as this one, or Forth screen files, can be used interchangably. 
  23.         Any "included" screen file is read in via  an implicit "1 LOAD" 
  24.         command.
  25. [THEN]
  26.  
  27. \ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  28. \                                    \
  29. \  If you like ForthCMP, please register it and get the programmers    \
  30. \  manual plus a disk (specify size) with the current version and    \
  31. \  additional support code:                        \
  32. \                                     \
  33. \  Send a check or money order for $50.00 US to:            \
  34. \      Tom Almy                                \
  35. \      17830 SW Shasta Trail                        \
  36. \      Tualatin, OR 97072                        \
  37. \                                                                       \
  38. \ Don't forget to request either the ANSI or 1983 standard versions,    \
  39. \ or order both versions at the same time for $75!                      \
  40. \                                     \
  41. \ (To get the Software Floating Point files (83 Standard version only), \
  42. \  you must prove ownership of an LMI Forth Software Floating Point by  \
  43. \  enclosing a copy of the first page in the manual section for         \
  44. \  Software Floating Point.)                                            \
  45. \ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  46.  
  47. \ I80186    \ Uncomment this if you are using an 80186 or later processor
  48.  
  49. 200 SEPSSEG    \ If you uncomment this line, then the program will have a
  50.         \ separate stack segment, 200 bytes long, to hold the
  51.         \ parameter and return stacks. This directive must come
  52.         \ before the MSDOS or MSDOSEXE directive
  53.  
  54. \ ForthCMP programs must start with one of the following directives:
  55.  
  56. 100 MSDOS \ Specifies to generate a COM file, allowing for a return stack
  57.           \ size of 100 bytes. This generates a "tiny" model program.
  58. \ 1000 100 MSDOSEXE \ Specifies to generate an EXE file, allowing 1000 bytes
  59.         \ for the data segment and 100 bytes for the return stack. This 
  60.         \ generates a small model program.
  61.  
  62. \ A maximal sized program can have 63k of code, 64k of static (dictionary)
  63. \ data, and 64k of stack data. You can request additional memory segments
  64. \ from DOS for large data arrays, for instance. Except when SEPSSEG is
  65. \ specified, the parameter stack moves down into the heap. The heap can
  66. \ be allocated using HERE (or DP) and ALLOT, just like in interpreted 
  67. \ Forth. PAD floats above HERE.
  68.  
  69. \ A DRIVER option allows creating device drivers in ForthCMP.
  70.  
  71. \ There is also an option for creating ROMMABLE code for embedded 
  72. \ processors.
  73.  
  74.  
  75. \ A ForthCMP program always contains a function called MAIN that acts as
  76. \ the main entry point. ForthCMP allows forward referencing, so we can safely
  77. \ put the MAIN function first.
  78.  
  79. 1 1 IN/OUT NEED TwoTimes    \ this tells the compiler that a forward
  80.                 \ referenced function has exactly one argument
  81.                 \ and one result value -- this allows the
  82.                 \ compiler to pass the argument and result
  83.                 \ in registers.
  84.                 \ The default is on the stack
  85.  
  86.  
  87. : MAIN
  88.     ." HELLO NEW USER!" CR CR    \ this should look standard.
  89.     ." Type a number:" #IN
  90.     ." Two times the number is: " TwoTimes . CR
  91.     ." Multiplication table"
  92.     MultTable
  93. ;                    \ returns to DOS at the end. Make
  94.                     \ certain the stack is empty, or
  95.                     \ push a 0 on stack if not sure.
  96.                     \ You can also execute BYE at any
  97.                     \ point to return. You can even
  98.                     \ return error codes, if you wish.
  99.  
  100. 1 1 IN/OUT    \ must match any forward declaration
  101.  
  102. CODE TwoTimes    \ We will use an assembly code routine to do the multiply
  103.     AX AX ADD    \ arg and result use AX
  104.     RET        \ couldn't be easier!
  105. END-CODE
  106.  
  107.  
  108. : MultTable    \ We didn't use IN/OUT here
  109.     11 1 DO
  110.         CR
  111.         11 1 DO
  112.         I J *  5 .R    \ print each entry
  113.     LOOP
  114.     LOOP
  115. ;
  116.  
  117. \ Our program is done. Now we need to include the library of ANSI standard
  118. \ and other functions that are not intrinsically generated.
  119. \ The generated load map shows these functions, as well as the ones above.
  120. \ You can use DEBUG to examine the generated code
  121.  
  122. INCLUDE FORTHLIB
  123.  
  124. END    \ Required last command -- writes out the compiled program
  125.  
  126. \ Now from the command line, execute "4c demo"
  127.